package com.wt.servlet;


import com.wt.controller.WqunCommunityController;
import com.wt.entity.WqunCommunity;
import com.wt.util.R.R;
import com.wt.util.ResultMess.ResultMess;
import com.wt.util.axios.Axios;
import com.wt.util.axios.JsonToEntity;
import com.wt.util.axios.Return;
import com.wt.util.exceptionhandler.WQUNException;
import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

@WebServlet({"/admin/wqunCommunity/findAllCommunityFirst","/admin/wqunCommunity/findAllCommunity",
            "/admin/wqunCommunity/addCommunity","/admin/wqunCommunity/deleteCommunityById",
        "/admin/wqunCommunity/adminDeleteCommunityById","/admin/wqunCommunity/admin/communityUserMess"})
public class CommunityServlet extends HttpServlet {
    WqunCommunityController wqunCommunityController = new WqunCommunityController();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject jsonObject = null;
        HashMap<String, String> map = null;
        WqunCommunity wqunCommunity = new WqunCommunity();

        //System.out.println(req.getRequestURL().substring(req.getRequestURL().lastIndexOf("/")+1));
        switch (req.getRequestURL().substring(req.getRequestURL().lastIndexOf("/")+1)){
            case "findAllCommunityFirst":
                Return.R(resp,wqunCommunityController.findAllCommunityFirst());
                break;
            case "findAllCommunity":
                Return.R(resp,wqunCommunityController.findAllCommunity());
                break;
            case "addCommunity":
                try {
                    jsonObject = Axios.post(req);//Json接受
                } catch (IOException e) {
                    Return.R(resp, R.error().message(ResultMess.DARA_FORMAT_ERROR.getDesc()));
                }
                try {
                    map = JsonToEntity.wqunCommunity(jsonObject);//将json转换成Object
                } catch (WQUNException e) {
                    Return.R(resp,R.error().message(e.getMsg()));//异常返回
                }

                try {
                    BeanUtils.populate(wqunCommunity,map);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }

                Return.R(resp,wqunCommunityController.addCommunity(wqunCommunity));
                break;
            case "deleteCommunityById":
                int id = Integer.parseInt(req.getParameter( "id"));
                Return.R(resp,wqunCommunityController.deleteCommunityById(id));
                break;
            case "adminDeleteCommunityById":
                int Did = Integer.parseInt(req.getParameter( "id"));
                Return.R(resp,wqunCommunityController.adminDeleteCommunityById(Did));
                break;
            case "communityUserMess":
                Return.R(resp,wqunCommunityController.communityUserMess());
                break;
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
